package com.bee.剑指offer;

/**
 * 输入一棵二叉树，求该树的深度。
 * 从根结点到叶结点依次经过的结点（含根、叶结点）形成树的一条路径，最长路径的长度为树的深度。
 *
 * @Author yong.Mr
 * @data 2021-04-23
 * @version 1.0.0
 */
public class 二叉树的深度 {

    /**
     * 计算二叉树的深度
     * @param root
     */
    public static int TreeDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int left = TreeDepth(root.left);
        int right = TreeDepth(root.right);
        return (left > right) ? (left + 1):(right + 1);
    }

    int leafCount = 0;

    /**
     * 计算叶子节点
     */
    public void countLeaf(TreeNode root) {
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            leafCount++;
        }
        countLeaf(root.left);
        countLeaf(root.right);
    }


    int sumCount = 0;

    /**
     * 计算树的总节点
     */
    public void sumLeaf(TreeNode root){
        if(root == null) {
            return;
        }
        sumCount++;
        sumLeaf(root.left);
        sumLeaf(root.right);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(10);
        root.left = new TreeNode(5);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(7);
        root.right = new TreeNode(2);
        int depth = TreeDepth(root);
        System.out.println(depth);
    }
}
